package com.atguigu.system.service.impl;

import com.atguigu.common.helper.MenuHelper;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.model.vo.AssignMenuVo;
import com.atguigu.system.execption.GuiguException;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysRoleMenuMapper;
import com.atguigu.system.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        //全部权限列表
        List<SysMenu> allList = this.list();
        if (CollectionUtils.isEmpty(allList)) {
            return null;
        }

        //构建树形数据
        List<SysMenu> sysMenuList = MenuHelper.buildTree(allList);
        return sysMenuList;
    }

    @Override
    public boolean removeById(Serializable id) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        int count = this.count(queryWrapper);
        if(count>0){ //当前节点有孩子节点，不允许删除
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        int i = sysMenuMapper.deleteById(id);
        return i==1?true:false;
    }

    @Override
    public List<SysMenu> getRoleMenuList(Long roleId) {
        //1.查询所有权限(菜单),status=0不要
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<SysMenu> sysMenuList = sysMenuMapper.selectList(queryWrapper);

        //3.处理所有节点需不需要打钩,查中间表数据，即：该角色已经拥有的权限列表
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));

        //存放已经拥有的权限id
        List<Long> menuIdsByRole = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
            menuIdsByRole.add(sysRoleMenu.getMenuId());
        }

        for (SysMenu sysMenu : sysMenuList) { //判断每一个节点是否打钩
            if(menuIdsByRole.contains(sysMenu.getId())){ //打钩
                sysMenu.setSelect(true);
            }else{//不用打钩
                sysMenu.setSelect(false);
            }
        }

        //2.构建树结构
        List<SysMenu> sysMenuTree = MenuHelper.buildTree(sysMenuList);
        return sysMenuTree;
    }

    @Override
    public void assignMenu(AssignMenuVo vo) {
        Long roleId = vo.getRoleId();
        List<Long> menuIdList = vo.getMenuIdList();
        //1.删除旧关系数据
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id",roleId));
        //2.增加新的关系数据
        if(!CollectionUtils.isEmpty(menuIdList)){
            for (Long menuId : menuIdList) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                sysRoleMenuMapper.insert(roleMenu);
            }
        }
    }

}
